function paths = date2folder(root, dates)
  % Convert an array of dates to standardized directory names under root,
  % bypassing dates that already have a directory named after them
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Release date: 
  
  try
      od = pwd;
      cd(root);
      N = numel(dates);
      paths = cell(N,1);
      [Y, M, ~, ~, ~, ~] = datevec(dates);
      for i=1:N
          try
              nd = fullfile(root, ['Y' num2str(Y(i))], ['M' num2str(M(i),...
                                                        '%02d')]);
              if isdir(nd)
                  paths{i} = nd;
              else
                  [s, m, ~] = mkdir(nd);
                  if s
                      paths{i} = nd;
                  else
                      throw(MException('date2folder:mkdir',...
                                      ['i: ' num2str(i) char(9)...
                                       'nd: ' regexprep(nd, '\\', '\\\\')...
                                       char(10) 'Message: ' m]))
                  end
              end
          catch ME
              tmp = ['date2folder error: root: ' root char(10)];
              paths{i} = [tmp ME.message];
          end
      end
      cd(od);
  catch ME
      tmp = ['date2folder error: root: ' root char(10)];
      paths = {[tmp ME.message]};
      cd(od);
  end
  
end